package com.ruoyi.client.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.client.pojo.form.GoodsQueryPageForm;
import com.ruoyi.client.pojo.vo.GoodsDetailVo;
import com.ruoyi.client.pojo.vo.GoodsVo;
import com.ruoyi.client.pojo.vo.PageListVo;
import com.ruoyi.client.service.GoodsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Anonymous
@CrossOrigin
@RestController("ClientGoodsController")
@Api(value = "v1", tags = "04.商品模块")
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    @Resource(name = "GoodsServiceImpl")
    protected GoodsService goodsService;

    /**
     * 查询商品分页列表
     */
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询商品分页列表", notes = "根据参数查询返回商品分页列表")
    @GetMapping("/page")  //分页查询Ruoyi框架使用的是Get
    public R<PageListVo<GoodsVo>> page(@Valid GoodsQueryPageForm form) { //参数不要加@RequestParam、@RequestBody等标注
        startPage();// 设置分页，父类BaseController有该方法
        List<GoodsVo> list = goodsService.queryPage(form); // 获取数据
        PageInfo<GoodsVo> pageInfo = new PageInfo<>(list);
        PageListVo<GoodsVo> pageListVo = new PageListVo<>();
        pageListVo.setTotalCount(pageInfo.getTotal());
        pageListVo.setTotalPage(pageInfo.getPages());
        pageListVo.setList(list);
        return R.ok(pageListVo);  // 接口返回
    }

    /**
     * 查询商品详情 ById
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询商品详情", notes = "传参为商品id")
    @GetMapping("/{id}")
    public R<GoodsDetailVo> goodsDetail(@PathVariable @ApiParam(value = "商品id", required = true) Long id) {

        GoodsDetailVo goodsDetailVo = goodsService.selectVoById(id);
        return R.ok(goodsDetailVo);
    }
}